<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/7/17
 * Time: 10:51
 */
namespace app\home\controller;
//include '/data/wwwroot/health.xuanwumobile.com/vendor/autoload.php'; // 引入 composer 入口文件
use think\Db;
use think\Config;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use EasyWeChat\Message\Text;
use EasyWeChat\Message\News;
use think\Controller;

class Server extends Common
{
    /**
     * 账号密码登录
     * @return bool
     * 参数：账号、密码
     * 返回值：0参数错误，1手机号码错误，2密码格式错误，3没有此用户，4密码错误，5成功
     */
    public function index()
    {
// 一些配置。
        $options = Config::get('wechat');
        // 使用配置来初始化一个项目。
        $app = new Application($options);
        $server = $app->server;
        /*-------------------------------------------------------------------------*/
        $menu = $app->menu;
        $buttons = [
            [
                "type" => "view",
                "name" => "红集商城",
                "url" => "http://mww5.xuanwumobile.com/home/Index/index"
            ]

        ];
        $menu->add($buttons);
        /*-------------------------------------------------------------------------*/
        $server->setMessageHandler(function ($message) {
            $openid = $message->FromUserName; // 用户的 openid...
            $ty = $message->MsgType; // 消息类型：event, text....
            //如果是关注事件
            $guanzhu = $message->Event;
            if($guanzhu == 'subscribe'){
                return "欢迎关注红集公众号！";
            }
            //============================
        });
        $response = $app->server->serve();
        // 将响应输出
        $response->send(); // Laravel 里请使用：return $response;
    }


    public function profile()
    {

        return $this->fetch('server/pay');
    }

    public function pay()
    {

        $options = Config::get('wechat');
        $app = new Application($options);
        $payment = $app->payment;
        //---------------------------2
        $attributes = [
            'trade_type'       => 'JSAPI', // JSAPI，NATIVE，APP...
            'body'             => 'iPad mini 16G 白色',
            'detail'           => 'iPad mini 16G 白色',
            'out_trade_no'     => '121774515555561368018',
            'total_fee'        => 1, // 单位：分
            'notify_url'       => 'https://health.xuanwumobile.com/home/Server/payback', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'openid'           => 'obTEv1nwIoVL-G8oT3_6oxIuU7Qs', // trade_type=JSAPI，此参数必传，用户在商户appid下的唯一标识，
            // ...
        ];

        $order = new Order($attributes);
        //--------------------------------3
        $result = $payment->prepare($order);
        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
            $prepayId = $result->prepay_id;
            $json = $payment->configForPayment($prepayId);
            return view('server/done',['json'=>$json]);
        }else{
            echo 'error aaa aaaa;';
        }

    }

    /**
     * @return View
     */
    public function payback()
    {
        $options = Config::get('wechat');
        $app = new Application($options);
        $payment = $app->payment;
        $result = $payment->refund('121774515555561368018', '44441212122121211', 1); // 总金额 100 退款 100，操作员：商户号
        var_dump($result);
    }

    public function kefu()
    {

        $options = Config::get('wechat');
        $app = new Application($options);
        $staff = $app->staff;
        $a = $staff->lists();
        var_dump($a);

    }

}